package com.iris.nio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;

public class AioAcceptClientHandler implements CompletionHandler<Void, AioAttach> {

    private AsynchronousSocketChannel socketChannel;
    public AioAcceptClientHandler(AsynchronousSocketChannel socketChannel ){
        this.socketChannel = socketChannel;
    }
    @Override
    public void completed(Void result, AioAttach attachment) {

        System.out.println("====AioAcceptClientHandler completed" +attachment.getId());

        try {
            System.out.println("completed:"+attachment.getId());
            socketChannel.write(ByteBuffer.wrap("reply:".getBytes())).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void failed(Throwable exc, AioAttach attachment) {
        System.out.println("====AioAcceptClientHandler completed" );

    }
}
